/*
 * MultiRecyclerContainer.java
 * business
 *
 * Created by ChenTao on 2017/3/2.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.recyclerview;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

import in.srain.cube.views.loadmore.LoadMoreContainerBase;

/**
 * RecyclerView用于加载更多的Container
 *
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/3/2 10:56
 */
public class LoadMoreRecyclerContainer extends LoadMoreContainerBase {

    private RecyclerView mRecyclerView;

    private MultiTypeLoadMoreAdapter mAdapter;

    private RecyclerView.OnScrollListener mOnScrollListener;

    private LinearLayoutManager mLinearLayoutManager;

    public LoadMoreRecyclerContainer(Context context) {
        super(context);
    }

    public LoadMoreRecyclerContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        if (isInEditMode()) {
            return;
        }

        View view = getChildAt(0);
        mRecyclerView = (RecyclerView) view;
        mRecyclerView.addOnScrollListener(
                new RecyclerView.OnScrollListener() {

                    // for memory profile
                    int mFirstVisibleItem, mVisibleItemCount, mTotalItemCount;

                    @Override
                    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                        if (mOnScrollListener != null) {
                            mOnScrollListener.onScrollStateChanged(recyclerView, newState);
                        }
                    }

                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        if (mOnScrollListener != null) {
                            mOnScrollListener.onScrolled(recyclerView, dx, dy);
                        }
                        getLinearLayoutManager();
                        if (mLinearLayoutManager == null) {
                            return;
                        }

                        mVisibleItemCount = recyclerView.getChildCount();
                        mTotalItemCount = mLinearLayoutManager.getItemCount();
                        mFirstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

                        if ((mTotalItemCount - mVisibleItemCount)
                                <= (mFirstVisibleItem + mVisibleThreshold)) {
                            onReachBottom();
                        }
                    }
                });
    }

    protected LinearLayoutManager getLinearLayoutManager() {
        if (mLinearLayoutManager != null) {
            return mLinearLayoutManager;
        }
        if (mRecyclerView == null) {
            return null;
        }
        RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
        if (layoutManager instanceof LinearLayoutManager) {
            mLinearLayoutManager = (LinearLayoutManager) layoutManager;
            return mLinearLayoutManager;
        }
        return null;
    }

    public void setOnScrollListener(RecyclerView.OnScrollListener listener) {
        mOnScrollListener = listener;
    }

    public void setRecyclerViewAdapter(MultiTypeLoadMoreAdapter adapter) {
        mAdapter = adapter;
        getRecyclerView().setAdapter(adapter);
    }

    public MultiTypeLoadMoreAdapter getAdapter() {
        return mAdapter;
    }

    protected RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    @Override
    protected void addFooterView(View view) {
        mAdapter.addFooterView(view);
    }

    @Override
    protected void removeFooterView(View view) {
        mAdapter.removeFooterView(view);
    }

    @Override
    protected Object retrieveListView() {
        return getRecyclerView();
    }
}
